<?php
namespace App\Repository\Impl;

if (!defined('IN_PX'))
    exit;

use App\Repository\User;

class UserImpl implements User {

    private function __Repository($value = 'user') {}

    private function __Inject($db) {}

    private $_table = '`#@__@user`';

    /**
     * 用户信息
     * @param $memberId
     * @return mixed
     */
    public function find($memberId) {
        return $this->db->select('*')
            ->table($this->_table)
            ->where('`member_id` = ?')
            ->bind(array($memberId))
            ->find();
    }

    /**
     * @Title: findMemberNum
     * @param $memberId
     * @Description: TODO 根据某字段查找用户数量
     * @return mixed
     * @author TUGE
     * @date
     */
    public function findMemberNum($value, $where) {
        $where = '`'.$where.'` = ?';
        //$this->db->debug();
        return $this->db->table('`#@__@user`')
            ->where($where)
            ->bind(array($value))
            ->count();
    }

    /**
     * 用户信息
     * @param $account
     * @return mixed
     */
    public function findAccount($account) {
        return $this->db->select('*')
            ->table($this->_table)
            ->where('`mobile` = ?')
            ->bind(array($account))
            ->find();
    }

    /**
     * @Title: findAnwserList
     * @param $memberId
     * @Description: TODO 查找用户答题记录列表
     * @return mixed
     * @author TUGE
     * @date
     */
    public function findAnwserList($memberId) {
        return $this->db->select('s.*, a.`title`')
            ->table('`#@__@score` s, `#@__@archives` a')
            ->where('s.`member_id` = ? AND s.`score` != -1 AND s.`archives_id` = a.`archives_id`')
            ->bind(array($memberId))
            ->findAll();
    }

    /**
     * 新用户注册
     * @param $post
     * @return bool
     */
    public function add($post) {
        $this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                '`mobile`' => '?',
                '`real_name`' => '?',
                '`password`' => '?',
                '`company`' => '?',
                '`reg_time`' => '?',
                '`is_status`' => '?',
                '`guid`' => '?',
                '`last_log_date`' => '?',
                '`update_time`' => '?',
            ))
            ->bind($post)
            ->save();
    }

    /**
     * 添加用户IP
     * @param $memberId
     * @param $ip
     * @return bool
     */
    public function addIp($memberId, $ip) {
        $this->db->debug();
        return $this->db->table('`#@__@member_ip`')
            ->row(array(
                '`member_id`' => '?',
                '`ip_address`' => '?',
                '`update_time`' => time(),
            ))
            ->bind(array($memberId, $ip))
            ->save();
    }

    /**
     * 用户IP数量
     * @param $memberId
     * @return bool
     */
    public function getMemberIpCount($memberId) {
        //$this->db->debug();
        return $this->db->table('`#@__@member_ip`')
            ->where('`member_id` = ?')
            ->bind(array($memberId))
            ->count();
    }

    /**
     * 用户IP数量
     * @param $memberId
     * @return bool
     */
    public function isMemberIp($memberId, $ip) {
        //$this->db->debug();
        return $this->db->table('`#@__@member_ip`')
            ->where('`member_id` = ? AND `ip_address` = ?')
            ->bind(array($memberId, $ip))
            ->count();
    }

    /**
     * 更新用户信息
     * @param $post
     * @return bool
     */
    public function update($post) {
        //$this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                '`member_name`' => '?',
                '`sex`' => '?',
                '`real_name`' => '?',
                '`tel`' => '?',
                '`factory`' => '?',
                '`qq`' => '?',
                '`mobile`' => '?',
                '`email`' => '?',
                '`company`' => '?',

                '`update_time`' => '?',
            ))
            ->where('`member_id` = ?')
            ->bind($post)
            ->update();
    }

    /**
     * 更新用户密码
     * @param $post
     * @return bool
     */
    public function changepwd($post) {
        //$this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                '`password`' => '?',
                '`guid`' => '?',
                '`update_time`' => '?',
            ))
            ->where('`member_id` = ?')
            ->bind($post)
            ->update();
    }

    /**
     * 更新用户密码
     * @param $post
     * @return bool
     */
    public function updatePwd($post) {
        //$this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                '`password`' => '?',
                '`guid`' => '?',
            ))
            ->where('`mobile` = ?')
            ->bind($post)
            ->update();
    }


    /**
     * 查看用户填写的新手机号是否被他人使用中
     * @param $mobile
     * @param $memberId
     * @return mixed
     */
    public function mobileCount($mobile, $memberId){
        return $this->db->table($this->_table)
            ->where('`mobile` = ? AND `member_id` != ?')
            ->bind(array($mobile, $memberId))
            ->count();
    }


    /**
     * 查看用户填写的新邮箱是否被他人使用中
     * @param $email
     * @param $memberId
     * @return mixed
     */
    public function emailCount($email, $memberId){
        return $this->db->table($this->_table)
            ->where('`email` = ? AND `member_id` != ?')
            ->bind(array($email, $memberId))
            ->count();
    }

    /**
     * 更新用户绑定的邮箱
     * @param $email
     * @param $memberId
     * @return mixed
     */
    public function updateEmail($email, $memberId) {
        return $this->db->table($this->_table)
            ->row(array(
                '`email`' => '?'
            ))
            ->where('`member_id` = ?')
            ->bind(array($email, $memberId))
            ->update();
    }

    /**
     * 更新用户绑定的手机号
     * @param $mobile
     * @param $memberId
     * @return mixed
     */
    public function updateMobile($mobile, $memberId) {
        return $this->db->table($this->_table)
            ->row(array(
                '`mobile`' => '?'
            ))
            ->where('`member_id` = ?')
            ->bind(array($mobile, $memberId))
            ->update();
    }

    /**
     * 手机号是否存在
     * @param $mobile
     * @return mixed
     */
    public function exists($mobile) {
        return $this->db->table($this->_table)
            ->where('`mobile` = ?')
            ->bind(array($mobile))
            ->count();
    }

    /**
     * 记录登陆时间
     * @param $memberId
     * @return mixed
     */
    public function updateLogin($memberId) {
        return $this->db->table($this->_table)
            ->row(array(
                '`last_log_time`' => time(),
            ))
            ->where('`member_id` = ?')
            ->bind(array($memberId))
            ->update();
    }

    /**
     * 检查是否VIP
     * @param $memberId
     * @return mixed
     */
    public function checkVip($memberId) {
        return $this->db->table($this->_table)
            ->where('`member_id` = ? AND `is_vip` = ? AND `vip_time` >= ?')
            ->bind(array($memberId, 1, time()))
            ->count();
    }
    public function checkVip2($memberId) {
        return $this->db->table($this->_table)
            ->where('`member_id` = ? AND `is_vip2` = ? AND `vip_time2` >= ?')
            ->bind(array($memberId, 1, time()))
            ->count();
    }
    public function checkVip3($memberId) {
        return $this->db->table($this->_table)
            ->where('`member_id` = ? AND `is_vip3` = ? AND `vip_time3` >= ?')
            ->bind(array($memberId, 1, time()))
            ->count();
    }

    /**
     * VIP
     * @param $memberId
     * @return mixed
     */
    public function updateVip($memberId, $time) {
        return $this->db->table($this->_table)
            ->row(array(
                '`is_vip`' => '?',
                '`vip_time`' => '?',
                '`stages`' => 6
            ))
            ->where('`member_id` = ?')
            ->bind(array(1, $time, $memberId))
            ->update();
    }
    public function updateVip2($memberId, $time) {
        $this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                '`is_vip2`' => '?',
                '`vip_time2`' => '?',
                '`stages2`' => 6
            ))
            ->where('`member_id` = ?')
            ->bind(array(1, $time, $memberId))
            ->update();
    }
    public function updateVip3($memberId, $time) {
        return $this->db->table($this->_table)
            ->row(array(
                '`is_vip3`' => '?',
                '`vip_time3`' => '?',
                '`stages3`' => 6
            ))
            ->where('`member_id` = ?')
            ->bind(array(1, $time, $memberId))
            ->update();
    }

    /**
     * 是否收藏
     * @param $memberId
     * @param $archivesId
     * @return mixed
     */
    public function isFocus($memberId, $archivesId) {
        return $this->db->table('`#@__@member_focus`')
            ->where('`member_id` = ? AND `archives_id` = ?')
            ->bind(array($memberId, $archivesId))
            ->count();
    }

    /**
     * 收藏记录
     * @param $memberId
     * @param $start
     * @param $end
     * @return mixed
     */
    public function focusList($memberId, $start = null, $end = null) {
        return $this->db->select('mf.*, a.`cover`, a.`title`, a.`title_english`, a.`view_count`, a.`collection_count`')
            ->table('`#@__@member_focus` mf, `#@__@archives` a')
            ->where('mf.archives_id = a.archives_id AND mf.`member_id` = ?')
            ->order('mf.`add_time`')
            ->limit($start, $end)
            ->bind(array($memberId))
            ->findAll();
    }

    /**
     * 收藏记录总数
     * @param $memberId
     * @return mixed
     */
    public function getMemberFocusCount($memberId) {
        return $this->db->select()
            ->table('`#@__@member_focus` mf, `#@__@archives` a')
            ->where('mf.`archives_id` = a.`archives_id` AND mf.`member_id` = ?')
            ->bind(array($memberId))
            ->count();
    }

    /**
     * 添加收藏
     * @param $memberId
     * @param $archivesId
     * @return mixed
     */
    public function focus($memberId, $archivesId) {
        return $this->db->table('`#@__@member_focus`')
            ->row(array(
                '`member_id`' => '?',
                '`archives_id`' => '?',
                '`add_time`' => time(),
            ))
            ->bind(array($memberId, $archivesId))
            ->save();
    }

    /**
     * 删除收藏
     * @param $memberId
     * @param $archivesId
     * @return mixed
     */
    public function delFocus($memberId, $archivesId) {
        return $this->db->table('`#@__@member_focus`')
            ->where('`member_id` = ? AND `archives_id` = ?')
            ->bind(array($memberId, $archivesId))
            ->delete();
    }

    /**
     * 播放记录
     * @param $memberId
     * @param $start
     * @param $end
     * @return mixed
     */
    public function viewList($memberId, $start = null, $end = null) {
        $this->db->debug();
        return $this->db->select('mh.`history_id`, mh.`archives_id`, mh.`video_id`, a.`cover`, a.`category_id`, a.`view_count`, a.`title`, a.`title_english`, a.`synopsis`, l.`lector_name`, '
            . ' v.`title` as `v_title`, v.`subtitle` as `v_subtitle`, mh.`view_time`')
            ->table('`#@__@member_history` mh, `#@__@archives` a, `#@__@lector` l, `#@__@video` v')
            ->where('mh.`archives_id` = a.`archives_id` AND mh.`video_id` = v.`video_id` AND mh.`member_id` = ? AND l.`lector_id` = a.`lector_id`')
            ->order('mh.`view_time`')
            ->limit($start, $end)
            ->bind(array($memberId))
            ->findAll();
    }

    /**
     * 播放记录总数
     * @param $memberId
     */
    public function viewCount($memberId) {
        return $this->db->select()
            ->table('`#@__@member_history` mh, `#@__@archives` a, `#@__@lector` l, `#@__@video` v')
            ->where('mh.`archives_id` = a.`archives_id` AND mh.`video_id` = v.`video_id` AND mh.`member_id` = ? AND l.`lector_id` = a.`lector_id`')
            ->bind(array($memberId))
            ->count();
    }

    /**
     * 删除播放记录
     * @param $memberId
     * @param $historyId
     * @return mixed
     */
    public function delView($memberId, $historyId) {
        return $this->db->table('`#@__@member_history`')
            ->where('`member_id` = ? AND `history_id` = ?')
            ->bind(array($memberId, $historyId))
            ->delete();
    }

    /**
     * 删除全部播放记录
     * @param $memberId
     * @return mixed
     */
    public function delAllView($memberId) {
        return $this->db->table('`#@__@member_history`')
            ->where('`member_id` = ?')
            ->bind(array($memberId))
            ->delete();
    }

    /**
     * 添加播放记录
     * @param $memberId
     * @param $archivesId
     * @return mixed
     */
    public function addView($memberId, $archivesId, $videoId) {
        return $this->db->table('`#@__@member_history`')
            ->row(array(
                '`member_id`' => '?',
                '`archives_id`' => '?',
                '`video_id`' => '?',
                '`view_time`' => time(),
            ))
            ->bind(array($memberId, $archivesId, $videoId))
            ->save();
    }

    /**
     * 修改播放记录
     * @param $memberId
     * @param $archivesId
     * @param $videoId
     * @return mixed
     */
    public function updateView($memberId, $archivesId, $videoId) {
        return $this->db->table('`#@__@member_history`')
            ->row(array(
                '`video_id`' => '?',
                '`view_time`' => time()
            ))
            ->where('`member_id` = ? AND `archives_id` = ?')
            ->bind(array($videoId, $memberId, $archivesId))
            ->update();
    }

    /**
     * 播放记录是否存在
     * @param $memberId
     * @param $archivesId
     * @return mixed
     */
    public function viewExist($memberId, $archivesId) {
        return $this->db->table('`#@__@member_history`')
            ->where('`member_id` = ? AND `archives_id` = ?')
            ->bind(array($memberId, $archivesId))
            ->count();
    }

    /**
     * 获取购买记录
     * @param $memberId
     * @param $archivesId
     * @return bool
     */
    public function getMemberVideo($memberId, $archivesId) {
        return $this->db->select('*')
            ->table('`#@__@member_video`')
            ->where('`member_id` = ? AND `archives_id` = ?')
            ->bind(array($memberId, $archivesId))
            ->find();
    }

    /**
     * 添加购买记录
     * @param $memberId
     * @param $archivesId
     * @param $time
     * @return bool
     */
    public function addMemberVideo($memberId, $archivesId, $time) {
        return $this->db->table('`#@__@member_video`')
            ->row(array(
                '`member_id`' => '?',
                '`archives_id`' => '?',
                '`vip_time`' => '?',
                '`add_time`' => time()
            ))
            ->bind(array($memberId, $archivesId, $time))
            ->save();
    }

    /**
     * 修改购买记录
     * @param $memberId
     * @param $archivesId
     * @param $time
     * @return bool
     */
    public function updateMemberVideo($memberId, $archivesId, $time) {
        return $this->db->table('`#@__@member_video`')
            ->row(array(
                '`vip_time`' => '?'
            ))
            ->where('`member_id` = ? AND `archives_id` = ?')
            ->bind(array($time, $memberId, $archivesId))
            ->update();
    }

}